<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui-loadmore_line .weui-loadmore__tips {
    background-color: #eee;
  }
  .rj-cell.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }
  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }
  .rj-cell.note label{
    float: left;
  }
  .rj-cell.note span{
    width: 80%;
    display: inline-block;
  }
  .weui-cells {
    margin-top: 0;
  }
  .weui-cells_form {
    background: #fbfbfb;
  }
  .weui-cell {
    background: #fff;
  }
  #acts span {
    font-size: 14px;
  }
  .halfbtn {
    display: inline-block;
    width: 49%;
  }
  .weui-label{
    /*width: 130px;*/
    text-align: left;
  }

  .weui-select {
    height: auto;
    line-height: normal;
  }
  *{
    list-style:none;
  }
  .weui-cell_select {
    padding: 10px 15px;
  }
  #acts span {
    font-size: 14px;
  }
</style>
<template>
  <div class="comon">
    <div style="height: 50px; line-height: 50px; background: #f7f7f7; text-align: center; font-size: 16px; border-bottom: 1px solid #bdbbbc; position: relative">
      录入报废单
      <a href="javascript:;" @click="$router.back(-1)" style="position: absolute; left: 10px; top: 8px">
        <img src="@/assets/back.png" style="width: 20px" />
      </a>
    </div>
    <div class="weui-cells weui-cells_form">
      <form action="" method="post" id="formid">
        <div class="cellgroup">
          <div class="weui-cell weui-cell_select">
            <div class="weui-cell__hd"><label class="weui-label">库房</label></div>
            <div class="weui-cell__bd">
              <input type="hidden"  name="entity.srguid" :value="room.guid" />
              <select class="weui-select" @change='getSpec()' v-model="room">
                <option  v-for="(item, index) in rooms" :key="item.guid" :value="item">{{item.name}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">库存组织</label></div>
            <div class="weui-cell__bd">
              <input type="hidden"  name="entity.orgguid" :value="room.orgguid" />
              <input type="text" name="entity.orgname" readonly class="weui-input orgguid" :value="room.orgname">
            </div>
          </div>
          <div class="weui-cell weui-cell_select">
            <div class="weui-cell__hd"><label class="weui-label">报废类型</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="entity.type" v-model="type">
                <option v-for="(item, index) in productTypes" :value="item.dictkey" >{{item.dicttext}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell weui-cell_select">
            <div class="weui-cell__hd"><label class="weui-label">报废原因</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="entity.scraptype">
                <option value="">请选择报废原因</option>
                <option v-for="(item, index) in scrapTypes" :value="item.dictkey" >{{item.dicttext}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">报废说明</label></div>
            <div class="weui-cell__bd">
              <textarea class="weui-textarea" name="entity.remark" placeholder="请输入报废说明" rows="2"></textarea>
            </div>
          </div>
        </div>
        <ScrapItem v-for="(item, index) in oitems" :key="index" :itemNo="item" :isLast="index+1 == oitems.length" :type="type" :houses="houses" :orgguid="room.orgguid" @doAddItem="addItem(index + 1)"
                      @doDelItem="delItem"></ScrapItem>
        <div class="weui-btn-area">
          <a class="weui-btn weui-btn_primary halfbtn subbtn"  href="javascript:" @click="send">提交</a>
          <a class="weui-btn weui-btn_primary halfbtn"  href="javascript:" @click="$router.back(-1)" id="sub">返回</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
  import ScrapItem from "./ScrapItem";

  export default {
    name: 'AddScrap',
    data () {
      return {
        openid: localStorage.openid,
        rooms: [],
        houses: [],
        ogs: {},
        type: 1,
        room: '',
        orgname: '',
        oitems: [0],
        productTypes: [],
        scrapTypes: []
      }
    },
    components: { ScrapItem },
    created() {
      window.addEventListener('setItem', ()=> {
        this.openid = localStorage.getItem('openid');
      })
    },
    mounted () {
      let that = this;
      //获取回显数据
      this.getBaseInfo();
      this.getProducttypes();
      this.getScraptypes();
    },
    watch: {
      'room.guid': function (val) {
        this.getHouse();
      },
      'type': function (val) {
        this.getHouse();
      },
    },
    methods: {
      addItem: function (index) {
        this.oitems.push(index);
      },
      delItem: function () {
        this.oitems.pop();
      },
      getBaseInfo: function() {
        this.axios({
          method: 'get',
          url: '/produce/Scrapapply!getBaseInfo.action',
          params: {}
        }).then((response) => {
          if (response.data.msg == 'success') {
            let data = response.data;
            this.rooms = data.rooms;
            this.room = data.rooms[0];
            this.ogs = data.ogs;
            //this.spes = data.spes;
          } else if (response.data.msg == 'outlogin') {
            this.toastText("请重新登录");
          } else if (response != undefined && response.data != undefined && response.data.msg == 'sessionTimeOut') {
          //不不设置提示
        } else {
            this.toastError("系统错误");
          }

        })
      },
      getHouse: function() {
        if (!this.checkNull(this.room) || this.type == undefined) {
          return;
        }
        this.axios({
          method: 'get',
          url: '/produce/StoreHouse!getHouses.action',
          params: {type: this.switchType(this.type), srguid: this.room.guid}
        }).then((response) => {
          if (response.data.msg == 'success') {
            let data = response.data;
            this.houses = data.houses;
          } else if (response != undefined && response.data != undefined && response.data.msg == 'sessionTimeOut') {
          //不不设置提示
        } else {
            this.toastError("系统错误");
          }

        })
      },
      getProducttypes: function() {
        let that = this;
        this.axios({
          method: 'get',
          responseType: 'json',
          url: '/sales/Sellform!findDict.action',
          params: {type: 98}
        }).then(response => {
          if (response.statusText == "OK") {
            that.productTypes = response.data;
          } else {
            this.toastText('获取货品类型数据失败');
          }
        });
      },
      getScraptypes: function() {
        let that = this;
        this.axios({
          method: 'get',
          responseType: 'json',
          url: '/sales/Sellform!findDict.action',
          params: {type: 95}
        }).then(response => {
          if (response.statusText == "OK") {
            that.scrapTypes = response.data;
          } else {
            this.toastText('获取报废原因数据失败');
          }
        });
      },
      send: function() {
        let that = this;

        if (!this.checkEmy('orgguid')) {
          this.toastText('公司有误！');
          return false;
        }
        if (!this.checkEmy('house')) {
          this.toastText('库间有误！');
          return false;
        }
        if (!this.checkEmy('product')) {
          this.toastText('产品有误！');
          return false;
        }
        if (!this.checkEmy('spec')) {
          this.toastText('规格有误！');
          return false;
        }
        if (!this.checkEmy('weight')) {
          this.toastText('重量有误！');
          return false;
        }
        //防止重复提交
        $('.subbtn').attr("disabled",true).css("pointer-events","none");
        //显示提交状态
        $.showLoading("正在提交");

        this.axios({
          method: 'post',
          url: '/produce/Scrapapply!save.action',
          //params: $("#formid").serialize()
          params: this.serialize2Obj($("#formid").serializeArray())
        }).then(response => {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          //关闭提交状态
          $.hideLoading();

            if(response.data.msg == "success"){
            this.toastText('操作成功!');
            this.$router.push({name: 'ScrapList'});
          }else if (response.data.msg == 'outlogin'){
            this.toastText('请重新登录!');
          }else if (response.data.msg == 'error'){
            this.toastError('系统错误!');
          }
        }).catch(function (error) {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          //关闭提交状态
          $.hideLoading();
          that.toastError('系统错误');
        });

      },
      switchType: function (type) {
        let realType = 0;
        switch (parseInt(type)) {
          case 1:
            realType = 3;
            break;
          case 2:
            realType = 1;
            break;
          case 3:
            realType = 2;
            break;
        }
        return realType;
      }
    }
  }


</script>
